#define _CRT_SECURE_NO_WARNINGS 1

class Solution
{
public:

    int check(int(&hash1)[26], int(&hash2)[26])
    {
        for (int i = 0; i < 26; i++)
        {
            if (hash1[i] != hash2[i])
            {
                return 0;
            }
        }

        return 1;
    }

    vector<int> findAnagrams(string s, string p)
    {
        vector<int> ret;
        int left = 0;
        int right = 0;
        int hash1[26]{ 0 };
        int hash2[26]{ 0 };

        for (int i = 0; i < p.size(); i++)
        {
            hash2[p[i] - 97]++;
        }

        while (right < s.size())
        {

            if (right - left == p.size())
            {
                if (1 == check(hash1, hash2))
                {
                    ret.push_back(left);
                }
                hash1[s[left] - 97]--;
                left++;
            }
            else if (right - left < p.size())
            {
                hash1[s[right] - 97]++;
                right++;
            }


        }
        if (right == s.size())
        {
            if (1 == check(hash1, hash2))
            {
                ret.push_back(left);
            }
        }
        return ret;
    }
};